package com.lz.service.impl;

import com.lz.entity.OrderMasterQuery;
import com.lz.enums.ResultEnum;
import com.lz.exception.SellException;
import com.lz.service.BuyerService;
import com.lz.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by lz on 2018/8/23.
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private OrderService orderService;

    @Override
    public OrderMasterQuery findOrderOne(String openid, String orderId) {
        return this.checkOrderOwner(openid,orderId);
    }

    @Override
    public OrderMasterQuery cancelOrder(String openid, String orderId) {
        OrderMasterQuery orderMasterQuery = this.checkOrderOwner(openid, orderId);
        if (orderMasterQuery == null){
            log.error("【取消订单】查不到该订单，orderId={}",orderId);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        return orderService.cancelOrder(orderMasterQuery);
    }

    public OrderMasterQuery checkOrderOwner(String openid, String orderId){
        OrderMasterQuery one = orderService.findOne(orderId);
        if (one == null){
            return null;
        }
        if (!one.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("【查询订单】openid不一致,openid={},orderId={}",openid,orderId);
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        return one;
    }
}
